package inf.loansManagement;

import inf.dvdsManagement.Dvd;
import inf.exceptions.*;
import inf.hibernateUtils.HibernateUtil;
import inf.mediasManagement.Media;
import inf.specimensManagement.Specimen;
import inf.subscribersManagement.Subscriber;

import java.util.List;


import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;


/** 
 * @author Fabre, Malric
 */
public class LoanDAO {

	public static final String TITLE = "title";

	public static boolean save(Loan loan) {
		inf.subscribersManagement.SubscriberDAO.add(loan.getSubscriber());
		inf.mediasManagement.MediaDAO.addSpecimen(loan.getSpecimen());

		Session session = HibernateUtil.openSession();
		try{
			session.saveOrUpdate(loan);
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * Gives the content of table medias in DB
	 * 
	 * @return
	 */
	public static List<Loan> getContent() {
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from loans");
		List<Loan> loans = query.list();

		session.close();
		return loans;
	}

	public static boolean contains(Loan loan) throws LoanExistsException {
		Session session = HibernateUtil.openSession();
		if (loan == null) throw new LoanExistsException();
		Loan l = (Loan) session.get(Loan.class, loan.getId());
		if (l == null) return false;
		else return l.equals(loan);	
	}

	/**
	 * Gets an instance of a media by ID classification
	 * 
	 * @param mediaId
	 * @return media
	 * @throws MediaExistsException 
	 * @throws DvdExistsException
	 */
	public static Loan get(long loanId) throws LoanExistsException {
		Session session = HibernateUtil.openSession();
		Criteria criteria = session.createCriteria(Loan.class).add(Restrictions.eq("id", loanId));
		Loan loan = (Loan)criteria.uniqueResult();
		if (loan == null) throw new LoanExistsException();
		else
			return loan;
	}


	public static List<Loan> getBySpecimenId(long specimenId) {
		Session session = HibernateUtil.openSession();
		Criteria criteria = session.createCriteria(Loan.class).add(Restrictions.eq("specimen.id", specimenId));
		List<Loan> loans = (List<Loan>)criteria.list();

		return loans;
	}
	
	public static List<Loan> getByUserId(long userid) {
		Session session = HibernateUtil.openSession();		
		Criteria criteria = session.createCriteria(Loan.class).add(Restrictions.eq("subscriber.id", userid));
		List<Loan> loans = (List<Loan>)criteria.list();

		return loans;
	}


	public long size(){
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from loans");
		List<Loan> loans = query.list();
		return loans.size();
	}

}


